﻿<?php 
error_reporting(E_ERROR|E_PARSE);
session_start();
if($_SESSION["islogin"]!=='igiveyouthepower'){
echo "<script language=JavaScript>\r\n";
echo "alert('请先登录!');\r\n";
echo "location.href='../index.php'\r\n";
echo "</script>";
}else{
require_once('../config.php');
?>
<?php 
$conn = mysqli_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname);
$user_name=$_SESSION["managername"];
$lang=$_GET["lang"];
$bname=$_GET["bname"];
if($_SESSION["thetype"]=="管理员"){$sqll="select * from ph_".$lang." where bname='".$bname."' order by id asc";}else{$sqll="select * from ph_".$lang." where user_name='".$user_name."' and bname='".$bname."' order by id asc";}
$result=mysqli_query($conn,$sqll);
while($row=mysqli_fetch_array($result)){
$sqlb="select * from ph_upc where user_name='".$user_name."' order by id asc limit 1";
$results=mysqli_query($conn,$sqlb);
if(mysqli_num_rows($results)<1){
echo "<script language=JavaScript>\r\n";
echo "alert('分配完成，SKU码不够!');\r\n";
echo "location.href='productslist.php?bname=".$bname."&lang=".$lang."'\r\n";
echo "</script>";
exit;
}else{
$rows=mysqli_fetch_array($results);
if(empty($row["skucode"])){
$sqls = "update ph_".$lang." set skucode='".$rows["upccode"]."' where ID='".$row["ID"]."'";
mysqli_query($conn,$sqls,$conn);
$sqlc = "delete from ph_upc where id ='".$rows["ID"]."'";
mysqli_query($conn,$sqlc,$conn);
}
}
//分配变体UPC
$sqlg="select * from ph_shuxing where proid='".$row["ID"]."' order by id asc";
$resultg=mysqli_query($conn,$sqlg);
while($rowg=mysqli_fetch_array($resultg)){
$sqld="select * from ph_upc where user_name='".$user_name."' order by id asc limit 1";
$resultd=mysqli_query($conn,$sqld);
if(mysqli_num_rows($resultd)<1){
echo "<script language=JavaScript>\r\n";
echo "alert('分配完成，SKU码不够!');\r\n";
echo "location.href='productslist.php?bname=".$bname."&lang=".$lang."'\r\n";
echo "</script>";
exit;
}else{
$rowd=mysqli_fetch_array($resultd);
if(empty($rowg["skucode"])){
$sqlp = "update ph_shuxing set skucode='".$rowd["upccode"]."' where ID='".$rowg["ID"]."'";
mysqli_query($conn,$sqlp);
$sqlu = "delete from ph_upc where id ='".$rowd["ID"]."'";
mysqli_query($conn,$sqlu);
}
}

}
}

echo "<script language=JavaScript>\r\n";
echo "alert('分配成功!');\r\n";
echo "location.href='productslist.php?bname=".$bname."&lang=".$lang."'\r\n";
echo "</script>";
?>
<?php }?>